public class SearchInsert {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6};
        int target = 5;
        SearchInsert searchInsert = new SearchInsert();
        System.out.println(searchInsert.searchInsert(nums, target));
    }

    public int searchInsert(int[] nums, int target) {
        // 定义左右索引
        int i = 0, j = nums.length - 1;
        // 循环 : i <= j的情况
        while (i <= j) {
            // 获取中间m索引值
            int m = (i + j) >>> 1;
            // 判断nums[m] 与target
            if (nums[m] < target) {
                i = m + 1;
            } else if (target < nums[m]) {
                j = m - 1;
            } else {
                // 相等时 , 继续向左判断
                j = m - 1;
            }
        }
        return i ;
    }


}
